<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>手写 call 方法，不借助原生的 call，apply，bind</h1>
    <script>
      Function.prototype.myCall = function (ctx, ...args) {
        // 获取this
        ctx = ctx === undefined || ctx === null ? globalThis : Object(ctx);
        const fn = this;
        const key = Symbol();
        Object.defineProperty(ctx, key, {
          value: fn,
          enumerable: false,
        });
        const result = ctx[key](...args);
        delete ctx[key];
        return result;
      };

      function fn(a, b) {
        console.log('args:', a, b);
        console.log('this', this);
        console.log(Object.keys(this));
      }

      const arr = [];

      fn.myCall({}, 1, 2);
    </script>
  </body>
</html>
